package com.yumingjiang.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.yumingjiang.common.annotation.Auth;
import com.yumingjiang.common.model.Result;
import com.yumingjiang.common.template.CommonCallback;
import com.yumingjiang.common.util.Precondition;
import com.yumingjiang.common.template.ControllerTemplate;
import com.yumingjiang.component.sentinel.SentinelBlockCallback;
import com.yumingjiang.service.UserService;
import com.yumingjiang.spi.LoadTestService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


/**
 * @description:
 * @author: John
 * @version: UserController.java, v1.0 2024年12月30日 03:05
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private ControllerTemplate controllerTemplate;

    @Autowired
    private UserService userService;

    @Autowired
    private LoadTestService loadTestService;

    @Auth//注解使用
    @SentinelResource(value = "queryById",
            blockHandlerClass = SentinelBlockCallback.class,
            blockHandler = "handleBlockException")
    @GetMapping("/queryById")
    public Result queryById(HttpServletRequest request, @RequestParam("id") String id) {
        return controllerTemplate.process(request, new CommonCallback() {//模板使用
            @Override
            public Result doBiz() {
                System.out.println("<id> "+loadTestService.load(id));
                Precondition.checkArgument(!"NULL".equals(id), "ID 不合法");
                return userService.queryById(id);
            }
        });
    }


}